package com.namo.ccharticle.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.namo.ccharticle.entity.Type;
import com.namo.ccharticle.service.TypeService;
import com.namo.cchcommon.api.CommonResult;
import com.namo.cchcommon.api.ResultCode;
import com.namo.cchcommon.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangXin
 * @date 2021/5/19 12:55
 */
@RestController
@RequestMapping("/type")
@Slf4j
public class TypeController {
    @Autowired
    Gson gson;

    @Autowired
    TypeService typeService;

    @RequestMapping(value = "/type_list", method = RequestMethod.GET)
    public CommonResult getTypeList() {
        return CommonResult.success(typeService.getTypeList());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult getTypeList(@RequestBody String req) {
        JsonObject reqObject = gson.fromJson(req, JsonObject.class);
        String name = null;
        Integer status = null;
        try {
            name = reqObject.get("name").getAsString();
            status = reqObject.get("status").getAsInt();
        } catch (Exception e) {
            log.error("请求参数解析错误=>" + e.getMessage());
            throw new ApiException(ResultCode.VALIDATE_FAILED);
        }
        log.info("新建分类成功");
        return CommonResult.success(typeService.addType(name, status));
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public CommonResult delType(@RequestBody String req) {
        JsonObject reqObject = gson.fromJson(req, JsonObject.class);
        String id = null;
        try {
            id = reqObject.get("id").getAsString();
        } catch (Exception e) {
            log.error("请求参数解析错误=>" + e.getMessage());
            throw new ApiException(ResultCode.VALIDATE_FAILED);
        }
        if (typeService.delType(id)) {
            log.info("删除分类成功=>" + id);
            return CommonResult.success(CommonResult.success("删除成功"));
        }
        return CommonResult.failed(ResultCode.FAILED, "删除失败");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult updateType(@RequestBody String req) {
        Type type = null;
        try {
            type = gson.fromJson(req, Type.class);
            if (type == null) {
                log.error("请求参数解析错误=>解析为空" );
                throw new ApiException(ResultCode.VALIDATE_FAILED);
            }
        } catch (JsonSyntaxException e) {
            log.error("请求参数解析错误=>" + e.getMessage());
            throw new ApiException(ResultCode.VALIDATE_FAILED);
        }
        if (typeService.updateType(type)) {
            log.info("更新分类成功=>");
            return CommonResult.success(CommonResult.success("更新成功"));
        }
        return CommonResult.failed(ResultCode.FAILED, "更新失败");
    }
}
